﻿using DevExpress.Web.ASPxGridView;
using System.Web.UI.WebControls;
using System.Linq;

namespace PmSadis.WebFormsUi.ExpandedControls
{
    public static class ExtASPxGridView
    {
        /// <summary>
        /// Задать начальные начтройки для грида
        /// </summary>
        /// <param name="gridView">ASPxGridView</param>
        public static void SetDefaultSettings(this ASPxGridView gridView)
        {
            gridView.Width = Unit.Percentage(100);

            gridView.Settings.ShowFilterRow = true;
            gridView.Settings.ShowFilterRowMenu = true;

            gridView.SettingsBehavior.ConfirmDelete = true;
            gridView.SettingsBehavior.AllowFocusedRow = true;

            gridView.SettingsPager.AlwaysShowPager = true;
            gridView.SettingsPager.Position = PagerPosition.TopAndBottom;

            gridView.SettingsPager.PageSize = 50;

            gridView.SettingsDetail.AllowOnlyOneMasterRowExpanded = true;

            gridView.SettingsEditing.EditFormColumnCount = 1;
            SetEditForms(gridView);

            gridView.Styles.AlternatingRow.Enabled = DevExpress.Utils.DefaultBoolean.True;

            gridView.CommandButtonInitialize += ExtASPxGridViewCommandButtonInitialize;
        }

        /// <summary>
        /// Инициализация коммандных кнопок
        /// </summary>
        /// <param name="sender">ASPxGridView</param>
        /// <param name="e"></param>
        private static void ExtASPxGridViewCommandButtonInitialize(object sender, ASPxGridViewCommandButtonEventArgs e)
        {
            if (Equals(e.ButtonType, ColumnCommandButtonType.ClearFilter))
                e.Image.Url = "~/Content/Images/Buttons/btnClearFiltersImage.png";
            else if (Equals(e.ButtonType, ColumnCommandButtonType.Delete))
                e.Image.Url = "~/Content/Images/Buttons/btnDelImage.gif";
            else if (Equals(e.ButtonType, ColumnCommandButtonType.Edit))
                e.Image.Url = "~/Content/Images/Buttons/btnEditImage.gif";
            else if (Equals(e.ButtonType, ColumnCommandButtonType.New))
                e.Image.Url = "~/Content/Images/Buttons/btnAddImage.gif";
        }

        /// <summary>
        /// Настроить окно редактирования модели
        /// </summary>
        /// <param name="gridView"></param>
        private static void SetEditForms(ASPxGridView gridView)
        {
            foreach (var column in gridView.Columns)
            {
                if (column is GridViewEditDataColumn)
                {
                    var col = column as GridViewEditDataColumn;
                    col.EditFormSettings.CaptionLocation = ASPxColumnCaptionLocation.Top;
                }
                if (column is GridViewCommandColumn)
                {
                    var col = column as GridViewCommandColumn;
                    col.ButtonType = ButtonType.Image;
                    col.Width = Unit.Pixel(50);
                    col.Caption = "Функции";
                    col.ClearFilterButton.Visible = true;
                }
            }
            foreach (var column in gridView.Columns.OfType<GridViewCommandColumn>())
            {
                
            }
        }
    }
}